package com.ruoyi.project.wenyanwen.generate.convert;


import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.project.dict.cizunew.domain.Cizu;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author jackyshi
 */
@Component
public abstract class BaseCizuConvert extends AbstractConvert {

    protected static Log logger = LogFactory.getLog(BaseCizuConvert.class);

    @Autowired
    @Qualifier("ssdbTemplate")
    RedisTemplate<String, String> redisTemplate;

    public static List<Map> RULE = null;

    public String replace(String s) {

        if (RULE == null) {
            String rule = redisTemplate.opsForValue().get("rule");
            RULE = JsonUtil.toListObject(rule, Map.class);
        }

        s = s.replace("\n", "").replace("\r", "");

        logger.debug(s);

        for (Map map : RULE) {
            String c = map.get("c").toString();
            String r = map.get("r").toString();
            s = s.replace(r, c);
        }
        return s;
    }


    private String buildCyjsHd(String cyjsHd) {
        cyjsHd = cyjsHd.replace("词语解释", "汉典解释");
        cyjsHd += "<p class=r><span class=hd><ci></ci></span></p>";
        return cyjsHd;
    }

    protected String buildDesc(Cizu cizu) {

        StringBuilder sb = new StringBuilder();

        //汉语词典(外研社)
        if (!StringUtils.isEmpty(cizu.getHycd())) {
            sb.append(cizu.getHycd());
        }

        if (!StringUtils.isEmpty(cizu.getCyjsHd())) {
            //2024-06-08 去掉汉典内容
            //sb.append(buildCyjsHd(cizu.getCyjsHd()));//
        }

        //国语词典
        if (!StringUtils.isEmpty(cizu.getGycd())) {
            sb.append(cizu.getGycd());
        }

        //历名解释
        if (!StringUtils.isEmpty(cizu.getLishi())) {
            sb.append(cizu.getLishi());
        }

        String s = sb.toString();
        return replace(s);

    }
}
